package com.hmdp.service.impl;

import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    private ShopTypeMapper shopTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 根据分类查询店铺
     * @return
     */
    @Override
    public List<ShopType> queryShopTpe()  {
       //1.先从redis中查询是由有数据存储
        List shopList = redisTemplate.opsForList().range(RedisConstants.SHOP_TYPE_KEY, 0, -1);
        //2.判断是否存在
        if (shopList.size() != 0) {
            //3.将数据进行返回
            return shopList;
        }
        //4.如果不存在，那么就需要在数据库里面进行查询
        List<ShopType> shopTypeList = new ArrayList<>();
       shopTypeList = shopTypeMapper.selectAll();

        if (shopTypeList.size() != 0) {
            //5.将数据缓存到redis里面，然后再返回
            redisTemplate.opsForList().rightPushAll(RedisConstants.SHOP_TYPE_KEY, shopTypeList);

        }
        return shopTypeList;
    }
}
